package com.example.sharding.jdbc.service.impl;

import com.example.sharding.jdbc.db.entity.PayOrderPO;
import com.example.sharding.jdbc.model.req.QueryPayOrderReq;
import com.example.sharding.jdbc.repository.PayOrderRepository;
import com.example.sharding.jdbc.service.PayOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goshine.common.resp.Page;
import com.goshine.common.resp.Response;
import com.goshine.common.util.BizUtils;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PayOrderServiceImpl implements PayOrderService {
    @Resource
    private PayOrderRepository payOrderRepository;

    public Response<PayOrderPO> queryByOrderId(long orderId) {
        return Response.success(payOrderRepository.queryByOrderId(orderId), "查询成功");
    }

    public Response<Integer> insert(PayOrderPO record) {
        return Response.success(payOrderRepository.insert(record), "新增成功");
    }

    @Transactional
    public Response<Integer> batchInsert(List<PayOrderPO> list) {
        return Response.success(payOrderRepository.batchInsert(list), "批量新增成功");
    }

    public Response<Integer> deleteByOrderId(long orderId) {
        return Response.success(payOrderRepository.deleteByOrderId(orderId), "删除成功");
    }

    public Response<Integer> updateByOrderId(PayOrderPO record) {
        return Response.success(payOrderRepository.updateByOrderId(record), "更新成功");
    }

    public Response<Page<PayOrderPO>> page(QueryPayOrderReq req) {
        PageInfo<PayOrderPO> pageInfo = PageHelper.startPage(req.getPageIndex(), req.getPageSize(), BizUtils.buildOrderByClause(req.getSortField(), req.getSortType()))
                .doSelectPageInfo(() -> payOrderRepository.queryByCond(req));

        return Response.success(Page.build(pageInfo, req), "查询成功");
    }


}
